package com.zhsource.system.controller;

import com.github.pagehelper.PageInfo;
import com.zhsource.auth.PermissionScanAnno;
import com.zhsource.exception.GlobalException;
import com.zhsource.system.domain.Employee;
import com.zhsource.system.query.EmployeeQuery;
import com.zhsource.system.service.IEmployeeService;
import com.zhsource.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>@ClassName：EmployeeController
 * <p>@Date 2023/10/17  10:21
 *
 * @Author zzl
 **/
@Slf4j
@Api(tags = "用户接口")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    // 查询所有
    @ApiOperation("查询所有")
    @GetMapping
    @PermissionScanAnno(name = "查询所有", sn="employee:list")
    @PreAuthorize("hasAuthority('employee:list')")
    public R<List<Employee>> list() {
        try {
            return R.ok("查询所有成功!", employeeService.list());
        } catch (GlobalException e) {
            log.error("Employee模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 查询单个

    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id获取用户信息")
    @PermissionScanAnno(name = "根据Id查询", sn = "employee:get")
    public R<Employee> selectById(@ApiParam(value = "用户ID", readOnly = true) @PathVariable("id") Long id) {
        try {
            return R.ok("查询单个成功!", employeeService.selectById(id));
        } catch (GlobalException e) {
            log.error("Employee模块的查询单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的查询单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 分页+ 模糊查询
    @PostMapping("/page")
    public R<PageInfo<Employee>> page(@RequestBody EmployeeQuery employeeQuery) {
        try {
            return R.ok("分页查询成功!", employeeService.page(employeeQuery));
        } catch (GlobalException e) {
            log.error("Employee模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }


    // 修改
    @PutMapping()
    public R<Void> update(@RequestBody Employee employee) {
        try {
            employeeService.update(employee);
            return R.ok("修改数据成功!");
        } catch (GlobalException e) {
            log.error("Employee模块的修改发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的修改发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 新增

    @PostMapping()
    @PermissionScanAnno(name = "新增员工", sn = "employee:insert")
    public R<Void> insert(@RequestBody Employee employee) {
        try {
            employeeService.insert(employee);
            return R.ok("新增数据成功!");
        } catch (GlobalException e) {
            log.error("Employee模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 删除单个

    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return R.ok("删除单个成功!");
        } catch (GlobalException e) {
            log.error("Employee模块的删除单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的查询删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 批量删除
    @PatchMapping("/batchDel")
    public R<Void> batchDel(@RequestBody List<Long> ids) {
        try {
            employeeService.batchDel(ids);
            return R.ok("批量删除成功!");
        } catch (GlobalException e) {
            log.error("Employee模块的批量删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Employee模块的批量删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
}
